import scenejson from "./scene.json";

let aa = {
  data() {
    return {};
  },
  methods: {
    earthopt() {
      earth.xbsjFromJSON(scenejson);

      // 113.562211 34.808985
      // 113.549628 34.808952
      // 113.549574 34.806160
      // 113.562248 34.806202

      let arr = [
        113.562211, 34.808985, 113.549628, 34.808952, 113.549574, 34.80616,
        113.562248, 34.806202, 113.562211, 34.808985,
      ];

      // let arr = [
      //   113.549628, 34.808952, 30, 113.562211, 34.808985, 30, 113.562248,
      //   34.806202, 30, 113.549574, 34.80616, 30,
      // ];

      let viewer = earth.czm.viewer;
      viewer.scene.globe.depthTestAgainstTerrain = true;
      // viewer.camera.setView({
      //   destination: new Cesium.Cartesian3(
      //     -2644963.9889313546,
      //     5763731.142118295,
      //     2199400.7089496767
      //   ), //世界坐标系下的一个坐标点
      //   orientation: {
      //     //旋转角度
      //     heading: 6.075,
      //     pitch: -0.727,
      //     roll: 6.283,
      //   },
      // });

      const extrudedPolygon = new Cesium.PolygonGeometry({
        polygonHierarchy: new Cesium.PolygonHierarchy(
          Cesium.Cartesian3.fromDegreesArray(arr)
        ),
        extrudedHeight: 1,
      });

      const instance = new Cesium.GeometryInstance({
        geometry: extrudedPolygon,
        id: "box with height",
      });

      const m = new Cesium.Material({
        fabric: {
          type: "zzzzzzzzz",
          uniforms: {
            texture: "/bg.png",
            normalTexture: "/bg.png",
            speed: 0.2,
            fixedFrameToEastNorthUpTransform: [
              0.9686, -0.16, 0.1904, 0, 0.2488, 0.6231, -0.7415, 0, 0, 0.7656,
              0.6433, 0, -0, 21058.9824, -6369376.5, 1,
            ],
            size: 100,
            waterColor: new Cesium.Color(1.0, 1.0, 1.0, 1.0),
            waterAlpha: 0.9,
            rf0: 0.3,
            lightDirection: new Cesium.Cartesian3(1.0, 0.0, 0.0),
            sunShiny: 100,
            distortionScale: 3.7,
          },
          source: `
          
uniform sampler2D texture; // 反射贴图
uniform sampler2D normalTexture; // 法线贴图
uniform float time;

uniform mat4 fixedFrameToEastNorthUpTransform; // 水面的东北天矩阵的逆矩阵

// 从顶点着色器传来的
varying vec4 v_worldPosition; // 当前像素的世界坐标
varying vec4 v_uv; // 原本的纹理坐标乘以贴图矩阵

// 可配置的参数
uniform float size; // 波纹大小（数值越大波纹越密集）
uniform vec4 waterColor; // 水面颜色
uniform float waterAlpha; // 水面透明度
uniform float rf0; // 水面反射率
uniform vec3 lightDirection; // 光照方向
uniform float sunShiny; // 光照强度
uniform float distortionScale; // 倒影的扭曲程度

vec3 sunDirection = normalize( lightDirection );
vec3 sunColor = vec3( 1.0 );


// 获取噪声
// vec4 czm_getWaterNoise(sampler2D normalMap, vec2 uv, float time, float angleInRadians)
vec4 getNoise( sampler2D normalMap, vec2 uv ) {
    vec2 uv0 = ( uv / 103.0 ) + vec2( time / 17.0, time / 29.0 );
    vec2 uv1 = uv / 107.0 - vec2( time / -19.0, time / 31.0 );
    vec2 uv2 = uv / vec2( 8907.0, 9803.0 ) + vec2( time / 101.0, time / 97.0 );
    vec2 uv3 = uv / vec2( 1091.0, 1027.0 ) - vec2( time / 109.0, time / -113.0 );
    vec4 noise = texture2D( normalMap, uv0 ) +
        texture2D( normalMap, uv1 ) +
        texture2D( normalMap, uv2 ) +
        texture2D( normalMap, uv3 );
    return noise * 0.5 - 1.0;
}

void sunLight( const vec3 surfaceNormal, const vec3 eyeDirection, float shiny, float spec, float diffuse, inout vec3 diffuseColor, inout vec3 specularColor ) {
    vec3 reflection = normalize( reflect( -sunDirection, surfaceNormal ) );  // 获得太阳对表面法线的反射向量
    float direction = max( 0.0, dot( eyeDirection, reflection ) );  // 当太阳反射方向和眼睛的方向一致时，direction 最大，为 1，当角度大于 90度时最小，最小为 0
    specularColor += pow( direction, shiny ) * sunColor * spec;
    diffuseColor += max( dot( sunDirection, surfaceNormal ), 0.0 ) * sunColor * diffuse;
}

czm_material czm_getMaterial(czm_materialInput materialInput) {
    czm_material material = czm_getDefaultMaterial(materialInput);

    // // 通过法线贴图计算新的表面法线
    // vec2 transformedSt = materialInput.st * 2.0 - 1.0;  // [0, 1] => [-1, 1]
    // vec4 noise = getNoise( normalTexture, transformedSt * size );
    // vec3 surfaceNormal = normalize( noise.xzy );  // [0, +1]，Y up

    // // 漫反射光
    // vec3 diffuseLight = vec3( 0.0 );
    // // 高光
    // vec3 specularLight = vec3( 0.0 );

    // // 获取视线方向（世界坐标）
    // vec3 eye = ( czm_inverseView * vec4( vec3(0.0), 1.0 ) ).xyz;
    // // 获取视线方向（水面的本地坐标）
    // eye = ( fixedFrameToEastNorthUpTransform * vec4( eye, 1.0) ).xyz;
    // // 当前像素的本地坐标
    // vec3 world = ( fixedFrameToEastNorthUpTransform * vec4( v_worldPosition.xyz, 1.0) ).xyz;

    // vec3 worldToEye = eye - world;  // east, north, up
    // worldToEye = vec3( worldToEye.x, worldToEye.z, -worldToEye.y );  // Y up
    // vec3 eyeDirection = normalize( worldToEye );

    // float shiny = sunShiny;
    // float spec = 2.0;
    // float diffuse = 0.5;
    // sunLight( surfaceNormal, eyeDirection, shiny, spec, diffuse, diffuseLight, specularLight );

    // float distance = length( worldToEye );
    // float distortionScale = distortionScale;
    // vec2 distortion = surfaceNormal.xz * ( 0.001 + 1.0 / distance ) * distortionScale;
    // vec3 reflectionSample = vec3( texture2D( texture, (v_uv.xy / v_uv.w) * 0.5 + 0.5 + distortion ) );

    // float theta = max( dot( eyeDirection, surfaceNormal ), 0.0 );
    // float rf0 = rf0;
    // float reflectance = mix( rf0, 1.0, pow( 1.0 - theta, 5.0 ) );

    // vec3 waterColor = waterColor.rgb;

    // // surfaceNormal 是以反射平面为 X-Y 平面的，
    // // 所以 eyeDirection 也得是以反射平面为 X-Y 平面。
    // vec3 scatter = max( 0.0, dot( surfaceNormal, eyeDirection ) ) * waterColor;
    // vec3 albedo = mix(
    //     sunColor * diffuseLight * 0.3 + scatter,
    //     vec3( 0.1 ) + reflectionSample * 0.9 + reflectionSample * specularLight,
    //     reflectance
    // );
    // material.diffuse = albedo.rgb;
    // material.alpha = waterAlpha;

    return material;
}

          `,
        },
      });

      const aper = new Cesium.MaterialAppearance({
        material: m,
      });

      var p = viewer.scene.primitives.add(
        new Cesium.Primitive({
          geometryInstances: instance,
          appearance: aper,
          releaseGeometryInstances: false,
          compressVertices: false,
        })
      );
    },
  },
};

export default aa;
